<?php

	extract($_GET);

if($action == "add"){
	 $sql = "select * from hy_b_sys_products where SysProductsID='".$prid."'";
	$objDb->query($sql);
	$row = $objDb->get_data();
	if(is_array($row)){
		$pname    = $row[0]['ProductName'];
		$num      = $row[0]['Unit'];
		$SC_Price = $row[0]['Price'];
		$buyNum   = $_GET['bn'];
		$money    = $price * $buyNum;
		if(empty($buyNum)){$buyNum = 1;}
	}
	$strtime = date("Y-m-d H:i:s",time());
    $sql     = "insert into `hy_b_car`(`memberID`,`productID`,`productName`,`price`,`buyNum`,`money`,`createdate`) values('".$_SESSION["user_id"]."','$prid','$pname','$num','$buyNum','$num','$strtime') ";
	
	$result  = $objDb->query($sql);

	if($result){
		$objBase->popup('成功放入兑换购物车!','m_cart.php');
		exit;
	} else {
		$objBase->popup('购买失败，请重试!');
		exit;
	}
} elseif ($action == "edit"){
	$buyNum = $_GET["bn"];
	$money  = $price*$buyNum;
	$sql    = "update hy_b_car set `buyNum`='$buyNum',`money`='$money' where id='$id' and memberID='".$_SESSION["user_id"]."' ";
	$result = $objDb->query($sql);
	if($result){
	   $objBase->popup('修改成功!');
		exit;
	} else {
		$objBase->popup('修改失败!');
		exit;
	}
} else if($action == "del"){
	if($id == "all"){
		$sql = "delete from hy_b_car where memberID='".$_SESSION["user_id"]."'";
	} else {
		$sql = "delete from hy_b_car where id=$id and memberID='".$_SESSION["user_id"]."'";
	}
	$result = $objDb->query($sql);
	if($result){
		$objBase->popup('删除成功!');
		exit;
	} else {
	   $objBase->popup('删除失败，请重试!');
	   exit;
	}
}

?>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td>
		  <form action="" method="get">
              <table class="order_table_1" width="100%" cellspacing="2" style="color:black">
                <tr valign="middle"  style="background-color:#CCCCCC">
                  <td width="16%" align="center">产品序号</td>
                  <td width="23%" align="center">产品名称</td>
                  <td width="13%" align="center">所需积分</td>
                  <td width="25%" align="center">数量</td>
                  <td width="15%" align="center">小计积分</td>
                  <td width="8%" align="center">操作</td>
                </tr>
         <?php
		$sql = "select * from hy_b_car where memberID='".$_SESSION["user_id"]."' ";
		$objDb->query($sql);
		$row = $objDb->get_data();
		if($row){
			$allmoney = 0;
			$i = 1;
			foreach($row as $rs){
				$allmoney += $rs["money"];
				$strprice  = $rs["price"];
				?>
                <tr valign="middle">
                  <td align="center"><?=$i?></td>
                  <td align="center" class="f_ff6c00"><a href="sub_product_detail.php?id=<?=$rs['productID']?>"><?=$rs["productName"]?></a></td>
                  <td align="center"><?=$strprice?></td>
                  <td align="center"><input name="buyNum<?=$rs["id"]?>" type="text" id="buyNum<?=$rs["id"]?>" value="<?=$rs["buyNum"]?>" size="12" maxlength="6" />
                  <input name="submit" type="button" value="修改" onclick="javascript:EditCar('<?=$rs["id"]?>','<?=$rs["price"]?>');" /></td>
                  <td align="center" class="f_ff6c00"><?=$rs["money"]?></td>
                  <td align="center"><a href="?id=<?=$rs["id"]?>&amp;action=del">删除</a></td>
                </tr>
              
			 
			
			<?php
			    $i++;
		    } 
		?>
		 
			    <tr valign="middle">
                  <td colspan="7" align="right" style="padding-right:20px; padding-top:5px; padding-bottom:5px;  border:none; color:#FF0000;">
				  所需总积分： <?=$allmoney?>
				  </td>
                </tr>
		   <tr valign="middle">
                  <td colspan="7" align="center" style="padding-right:20px; padding-top:15px; padding-bottom:10px;  border:none; color:#FF0000;">				                
				     <input class="order_form_5" value="兑换" type="button" onclick="javascript:location.href='cart_ok.php?action=pay&totalzhifen=<?=$allmoney?>';"/>
                     <input class="order_form_6" value="删除所有" type="button" onclick="javascript:location.href='?action=del&id=all';"  />
				  </td>
                </tr>
		<?		
		} else { ?>
                <tr valign="middle"><td colspan="6" align="center"><div align="center">暂无纪录</div></td></tr>
        <?php 
		}
		?>
          
              </table>
            </form>
			</td>
        </tr>
      </table>
	  
<script type="text/javascript">
<!--
function EditCar(s,p){
	var buyNum = document.getElementById("buyNum"+s).value;
	if(isNaN(buyNum) || buyNum.indexOf('.')!=-1){
		alert('数量必须为整数');
		document.getElementById("buyNum"+s).focus();
		return false;
	} else {
		location.href="?bn="+buyNum+"&id="+s+"&price="+p+"&action=edit";
	}
}
-->
</script>